package cn.yeziji.file.vo;

import cn.yeziji.forum.entity.ForumFileEntity;
import cn.yeziji.forum.utils.StrUtils;
import cn.yeziji.forum.utils.TimeUtils;
import cn.yzj.common.CommonSymbol;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * 数据文件视图对象
 *
 * @author gzkemays
 * @since 2022/11/04 3:25 PM
 */
@Data
public class FileDataVO {
  /** 文件名 */
  String filename;
  /** 上传者 */
  String source;
  /** 文件资源目标 */
  String target;
  /** 文件缩略图 */
  String image;
  /** 文件创建时间 */
  String createTime;

  public FileDataVO(ForumFileEntity entity) {
    String scope = entity.getScope();
    int lastStar;
    if (scope.contains("/") && (lastStar = scope.lastIndexOf(CommonSymbol.STAR_SIGN)) != -1) {
      this.filename = scope.substring(scope.indexOf("/") + 1, lastStar);
    }
    this.target = entity.getTarget();
    this.image = entity.getImage();
    this.source = entity.getSource();
    this.createTime = StrUtils.dateToStr(TimeUtils.COMMON_SIMPLE_PATTERN, entity.getCreateTime());
  }

  public static List<FileDataVO> buildByEntities(List<ForumFileEntity> entities) {
    List<FileDataVO> list = new ArrayList<>();
    for (ForumFileEntity entity : entities) {
      list.add(new FileDataVO(entity));
    }
    return list;
  }

  public static void main(String[] args) {
    String scope = "docs/面试题.xlsx*1";
    int lastStar;
    if (scope.contains("/") && (lastStar = scope.lastIndexOf(CommonSymbol.STAR_SIGN)) != -1) {
      System.out.println(scope.substring(scope.indexOf("/"), lastStar));
    }
  }
}
